package wt.hpad.utils.proj;

import android.content.Context;
import android.graphics.Path;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.widget.RelativeLayout;

/**
 * 动画工具
 * Created by wt on 12.31.
 */

public class AnimUtil {
    /**
     * 给view加上轨迹动画
     */
    public static void addPathAnimationByView(View targetView, final ViewGroup parentView, View endView, int resid, Context context, final IAnimHandler iAnimHandler) {
        // 屏幕左上坐标
        int parent[] = new int[2];
        parentView.getLocationInWindow(parent);

        // 动画内容 view 的坐标(抛物线起点坐标)
        int startLoc[] = new int[2];
        targetView.getLocationInWindow(startLoc);

        // 购物车坐标
        int endLoc[] = new int[2];
        endView.getLocationInWindow(endLoc);

        // 动画内容
        final View animContent = new View(context);
        animContent.setBackgroundResource(resid);  // 设置动画"内容"(图片)
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(75, 75);

        //
        parentView.addView(animContent, params);

        // 动画图片作为 动画起始位置 的绝对坐标
        float startX = startLoc[0] - parent[0] + targetView.getWidth() / 3;
        float startY = startLoc[1] - parent[1] + targetView.getWidth() / 2;

        // 购物车作为 动画结束位置 的绝对坐标
        float toX = endLoc[0] - parent[0] + endView.getWidth() / 3;     // 为了达到正入"框中"的效果
        float toY = endLoc[1] - parent[1];

        // 规划路径
        Path path = new Path();
        path.moveTo(startX, startY);
        path.quadTo(startX, startY, toX, toY);


        PathAnimation animation = new PathAnimation(path);
        animation.setDuration(500);
        animation.setInterpolator(new LinearInterpolator());
        animation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                iAnimHandler.animEnd();
                parentView.removeView(animContent);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
            }
        });
        // 运行动画
        animContent.startAnimation(animation);
    }

    /**
     * 给view加上轨迹动画
     */
    public static void addPathAnimationByPosition(View targetView, final ViewGroup parentView, int[] parentPos, int[] endPos, Drawable drawable, Context context, final IAnimHandler iAnimHandler) {
        // 屏幕左上坐标
        int parent[] = parentPos;

        // 动画内容 view 的坐标(抛物线起点坐标)
        int startLoc[] = new int[2];
        targetView.getLocationInWindow(startLoc);
        // 购物车坐标
        int endLoc[] = endPos;

        // 动画内容
        final View animContent = new View(context);
        animContent.setBackground(drawable);  // 设置动画"内容"(图片)
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(100, 100);


        //
        parentView.addView(animContent, params);

        // 动画图片作为 动画起始位置 的绝对坐标
        float startX = startLoc[0] - parent[0] + targetView.getWidth() / 3;
        float startY = startLoc[1] - parent[1] + targetView.getWidth() / 2;

        // 购物车作为 动画结束位置 的绝对坐标
        float toX = endLoc[0] - parent[0];     // 为了达到正入"框中"的效果
        float toY = endLoc[1] - parent[1];

        // 规划路径
        Path path = new Path();
        path.moveTo(startX, startY);
        path.quadTo(startX, startY, toX, toY);


        PathAnimation animation = new PathAnimation(path);
        animation.setDuration(1000);
        animation.setInterpolator(new LinearInterpolator());
        animation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                iAnimHandler.animEnd();
                parentView.removeView(animContent);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
            }
        });
        // 运行动画
        animContent.startAnimation(animation);
    }
}
